import axios from 'axios';
import { Message,Loading  } from 'element-ui';
import Vue from 'vue'
import router from '@/router/index'


Vue.config.productionTip = false
Vue.prototype.$axios = axios
const instance = axios.create({
  timeout: 10000,
})
// 错误整合 只显示最后一个
function lastError(val, error) {
  switch (val) {
    case 500:
      Message({ message: '请求发生错误,状态码:500', type: 'error' })
      break;
    case 405:
      Message({ message: '请求方法错误,状态码:405', type: 'error' })
      break;
    case 504:
      Message({ message: '系统未启动,状态码:504', type: 'error' })
      break;
    case 404:
      Message({ message: '路径错误,状态码:404', type: 'error' })
      break;
    case 808:
      Message({ message: '登录超时，请重新登录,状态码:808', type: 'error' })
      router.push('/login?corpId=$CORPID$')
      break;
    default:
      return Promise.reject(error)
  }
}
// 请求拦截器
instance.interceptors.request.use(config => {
  const token = sessionStorage.getItem('token')||0
  token && (config.headers.token = token);
  if (config.method === 'get') {
    if (config[0]) {
      config.url = config.url + '?' + config[0]
    }
  }
  return config
}, error => {
  return Promise.reject(error)
})
// 响应拦截器
instance.interceptors.response.use(response => {
  return response
}, error => {
  if (error.response) {
    lastError(error.response.status, error)
  }
  return Promise.reject(error)
})
export default instance